package com.arsn.myf.flow.controller;


import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试
 */
@RestController
@RequestMapping("/test1")
public class Test1FlowController {


    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;


    /**
     * 部署
     */
    @GetMapping("/testDeploy")
    public void testDeploy(){
        //部署
        Deployment deploy = repositoryService.createDeployment()
            .addClasspathResource("test1.bpmn20.xml")
                    .name("出差申请流程")
                    .category("出差申请流程")
                    .tenantId("000000")
                    .deploy();
        System.out.println("deploy ID= " + deploy.getId());
    }

    /**
     * 删除流程
     */
    @GetMapping("/delete")
    public void delete(){
        //删除流程--流程id
        repositoryService.deleteDeployment("589ca083-5849-11ed-89d2-005056c00001",true);
    }



    /**
     * 启动流程
     */
    @GetMapping("/testStart")
    public void testStart(){
        Map<String, Object> map = new HashMap<>();
        map.put("taskUser","a1");
        map.put("applyUser1","a2");
        map.put("applyUser2","a3");
        map.put("applyUser3","a4");
        //部署
        //流程实例id
        runtimeService.startProcessInstanceById("test1:1:1ff31b53-584f-11ed-8b35-005056c00001",map);
    }

    /**
     * A1完成任务
     */
    @GetMapping("/testComplete")
    public void testComplete(){
        //获取任务
        Task task = taskService.createTaskQuery()
                //流程实例id
                .processInstanceId("461b2c6e-584f-11ed-abe2-005056c00001")
                //任务处理人
                .taskAssignee("a1")
                .singleResult();
        Map<String, Object> processVariables = task.getProcessVariables();
        processVariables.put("num",2);
        //流程实例id
        taskService.complete(task.getId(),processVariables);
    }

    /**
     * A2完成任务
     */
    @GetMapping("/testComplete2")
    public void testComplete2(){
        //获取任务
        Task task = taskService.createTaskQuery()
                //流程实例id
                .processInstanceId("461b2c6e-584f-11ed-abe2-005056c00001")
                //任务处理人
                .taskAssignee("a2")
                .singleResult();
        //流程实例id
        taskService.complete(task.getId());
    }

    /**
     * A4完成任务
     */
    @GetMapping("/testComplete4")
    public void testComplete4(){
        //获取任务
        Task task = taskService.createTaskQuery()
                //流程实例id
                .processInstanceId("461b2c6e-584f-11ed-abe2-005056c00001")
                //任务处理人
                .taskAssignee("a4")
                .singleResult();
        //流程实例id
        taskService.complete(task.getId());
    }


}
